package com.cskaoyan.gateway.controller.user;

import com.cskaoyan.gateway.form.user.RegisterForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IUserService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: thz
 * @class: RegisterController
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class RegisterController {

    @Reference(timeout = 10000,check = false,retries = 0)
    IUserService userService;

    @Anoymous
    @PostMapping("/register")
    public ResponseData userRegister(@Valid @RequestBody RegisterForm registerForm, HttpServletRequest httpServletRequest){
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");

        UserRegisterRequest request = new UserRegisterRequest();
        request.setEmail(registerForm.getEmail());
        request.setUserName(registerForm.getUserName());
        request.setUserPwd(registerForm.getUserPwd());
        request.setCaptcha(registerForm.getCaptcha());

        UserRegisterResponse response = userService.userRegister(request,uuid);
        if (SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())){
            userService.sendEmail(registerForm.getEmail(),uuid,registerForm.getUserName());

            return new ResponseUtil().setData(null);

        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
